package com.cy.sys.serviceImpl;

import com.cy.common.annotation.RequiredLog;
import com.cy.sys.dao.UserDao;
import com.cy.sys.pojo.PageObject;
import com.cy.sys.pojo.sysUserDeptPojo;
import com.cy.sys.pojo.sysUserPojo;
import com.cy.sys.service.UserServcie;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.shiro.crypto.hash.SimpleHash;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author MTA
 */
@Service


public class UserServcieImpl implements UserServcie {
    @Autowired
    private UserDao userDao;



    @Override
    @RequiredLog(operation = "查询所有用户")
    @Cacheable(value = "userCache")
    @Transactional(readOnly = true)
    public PageObject doFindPageObjects(String username, Integer pageCurrent) {
            if(pageCurrent==null || pageCurrent==0) {
                throw  new IllegalArgumentException("错误");
            }
        Page<sysUserPojo> page= PageHelper.startPage(pageCurrent,5);
        List<sysUserPojo> records=userDao.doFindPageObjects(username);
        
        System.out.println("page.getTotal"+page.getTotal());
        System.out.println("Long.valueOf(page.getPages()"+Long.valueOf(page.getPages()));




        return new PageObject(Long.valueOf(pageCurrent),5,page.getTotal(),records);
    }

    @Override
    @RequiredLog(operation = "查询用户")
    @CacheEvict(value = "userCache",allEntries = true)
    public int validById(Integer id, Integer valid) {
        //判断id--没写
        String modifiedUser="hhh";
        userDao.validById(id,valid,modifiedUser);

        return 0;
    }

    @Override
    @RequiredLog(operation = "添加用户")
    @CacheEvict(value = "userCache",allEntries = true)
    @Transactional(readOnly = false)
    public int doSaveObject(sysUserDeptPojo sysUserDeptPojo,Integer... roleIds) {
        String username=sysUserDeptPojo.getUsername();
        String add=userDao.doFindIdByName(username);
        if(add!=null){

            throw new IllegalArgumentException("用户已存在");

        }
        String psw=sysUserDeptPojo.getPassword();
        String salt= UUID.randomUUID().toString();
            //加密顺序函数参数顺序===盐值方式，原密码，加密的随机后缀，加密次数
        SimpleHash sh=new SimpleHash("MD5",psw,salt,1);

        sysUserDeptPojo.setPassword(sh.toHex());
        sysUserDeptPojo.setSalt(salt);

         int a=   userDao.doSaveObject(sysUserDeptPojo);
                if(a<1){
                    throw new IllegalArgumentException("添加失败");
                }

        String id=userDao.doFindIdByName(username);
        for (Integer roleId : roleIds) {
                userDao.insertjs(roleId,Integer.valueOf(id));
        }

        return a;
    }

    @Override
    public Map<Object,Object> doFindObjectById(Integer id) {
        sysUserPojo user=userDao.doFindObjectById(id);
        List<Integer> roleIds=userDao.doFindqx(id);
        Map<Object,Object> map=new HashMap<>();
        map.put("user",user);
        map.put("roleIds",roleIds);

        return map;
    }

    @Override
    @RequiredLog(operation = "修改用户")
    @CacheEvict(value = "userCache",allEntries = true)
    public int doUpdateObject(sysUserDeptPojo sysUserDeptPojo, Integer... roleIds) {
       userDao.doUpdateObject(sysUserDeptPojo);
      int id= sysUserDeptPojo.getId();
        for (Integer roleId : roleIds) {
            userDao.insertjs(roleId,id);
        }
        return 0;
    }
}
